package com.nameless.controller;

import com.nameless.util.ValidateCodeUtils;
import com.nameless.vo.Result;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.redis.core.BoundGeoOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * 送味精
 * -*- coding:utf-8 -*-
 *
 * @author 太阳偷渡青山
 * @software IntelliJ IDEA
 * @date 2023/03/02 10:49
 * @Description
 */
@ApiModel(description = "短信验证 -*- coding:utf-8 -*-")
@RequestMapping("/common/sendMsg")
@RestController
@Slf4j
public class SendMsg {
    @ApiModelProperty(hidden = true)
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 短信验证
     *
     * @param phoneNum 电话
     * @return {@link Result}<{@link String}>
     */
    @GetMapping
    @ApiImplicitParam("短信验证")
    public Result<String> sendMsg(String phoneNum){

        if(StringUtils.isNotEmpty(phoneNum)){
            //生成随机的4位验证码
            String code = ValidateCodeUtils.generateValidateCode(6).toString();
//            log.info("code = {}",code);
            System.out.println(phoneNum+"==============");
            //调用阿里云提供的短信服务API完成发送短信
            //SMSUtils.sendMessage("瑞吉外卖","",phone,code);

            // 将数据保存到redis数据库中
            stringRedisTemplate.opsForValue().set(phoneNum,code,1, TimeUnit.MINUTES);

//            System.out.println(stringStringBoundGeoOperations+"**********************");
            //需要将生成的验证码保存到Session
//            WebUtils.getRequest().getSession().setAttribute(phone,code);

            return Result.success("手机验证码短信发送成功",code);
        }
        return Result.error("短信发送失败");
    }
}
